<template>
  <!-- 课程信息表单组件 -->
  <div>
    <!-- 使用Element Plus的表单组件，绑定表单数据、验证规则等 -->
    <el-form ref="formRef" :model="formData" :rules="rules" size="default" label-width="100px">
      <!-- 第一行：课程编码和课程学科 -->
      <el-row gutter="15">
        <el-col :span="10">
          <!-- 课程编码输入框 -->
          <el-form-item label="课程编码" prop="code">
            <el-input v-model="formData.code" type="text" placeholder="请输入课程编码" clearable
              :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <!-- 课程学科下拉选择框 -->
          <el-form-item label="课程学科" prop="subject">
            <el-select v-model="formData.subject" placeholder="请选择课程学科" clearable :style="{width: '100%'}">
              <el-option v-for="(item, index) in subjectOptions" :key="index" :label="item.label"
                :value="item.value" :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 第二行：课程名称和课程价格 -->
      <el-row gutter="15">
        <el-col :span="10">
          <!-- 课程名称输入框 -->
          <el-form-item label="课程名称" prop="field108">
            <el-input v-model="formData.field108" type="text" placeholder="请输入课程名称" clearable
              :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <!-- 课程价格输入框 -->
          <el-form-item label="课程价格" prop="field109">
            <el-input v-model="formData.field109" type="text" placeholder="请输入课程价格" clearable
              :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 第三行：适用人群和课程有效期 -->
      <el-row gutter="15">
        <el-col :span="10">
          <!-- 适用人群输入框 -->
          <el-form-item label="适用人群" prop="field110">
            <el-input v-model="formData.field110" type="text" placeholder="请输入适用人群" clearable
              :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <!-- 课程有效期日期选择器 -->
          <el-form-item label="课程有效期" prop="field111">
            <el-date-picker type="daterange" v-model="formData.field111" format="YYYY-MM-DD"
              value-format="YYYY-MM-DD" :style="{width: '100%'}" start-placeholder="开始日期"
              end-placeholder="结束日期" range-separator="至" clearable></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 第四行：课程封面上传和推荐指数 -->
      <el-row gutter="15">
        <el-col :span="10">
          <!-- 课程封面上传组件 -->
          <el-form-item label="课程封面" prop="field112" required>
            <el-upload ref="field112" :file-list="field112fileList" :action="field112Action"
              :before-upload="field112BeforeUpload">
              <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <!-- 推荐指数评分组件 -->
          <el-form-item label="推荐指数" prop="field113">
            <el-rate v-model="formData.field113"></el-rate>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 第五行：课程介绍文本域 -->
      <el-row gutter="15">
        <el-col :span="18">
          <!-- 课程介绍多行文本输入框 -->
          <el-form-item label="课程介绍" prop="field114">
            <el-input v-model="formData.field114" type="textarea" placeholder="请输入课程介绍"
              :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 表单操作按钮 -->
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup>
// 引入Element Plus的消息提示组件
import {
  ElMessage
}
from 'element-plus'

// 创建表单引用
const formRef = ref()

// 使用reactive创建响应式数据对象
const data = reactive({
  // 表单数据对象
  formData: {
    code: undefined,          // 课程编码
    subject: undefined,       // 课程学科
    field108: undefined,      // 课程名称
    field109: undefined,      // 课程价格
    field110: undefined,      // 适用人群
    field111: null,           // 课程有效期
    field112: null,           // 课程封面
    field113: 0,              // 推荐指数
    field114: undefined,      // 课程介绍
  },
  // 表单验证规则
  rules: {
    code: [{
      required: true,
      message: '请输入课程编码',
      trigger: 'blur'
    }],
    subject: [{
      required: true,
      message: '请选择课程学科',
      trigger: 'change'
    }],
    field108: [{
      required: true,
      message: '请输入课程名称',
      trigger: 'blur'
    }],
    field109: [{
      required: true,
      message: '请输入课程价格',
      trigger: 'blur'
    }],
    field110: [{
      required: true,
      message: '请输入适用人群',
      trigger: 'blur'
    }],
    field111: [{
      required: true,
      message: '课程有效期不能为空',
      trigger: 'change'
    }],
    field113: [{
      required: true,
      message: '推荐指数不能为空',
      trigger: 'change'
    }],
    field114: [{
      required: true,
      message: '请输入课程介绍',
      trigger: 'blur'
    }],
  }
})

// 解构获取表单数据和验证规则
const {
  formData,
  rules
} = toRefs(data)

// 课程学科选项数据
const subjectOptions = ref([{
  "label": "选项一",
  "value": 1
}, {
  "label": "选项二",
  "value": 2
}])

// 上传请求路径
const field112Action = ref('https://jsonplaceholder.typicode.com/posts/')
// 上传文件列表
const field112fileList = ref([])

/**
 * @name: 上传之前的文件判断
 * @description: 上传之前的文件判断，判断文件大小文件类型等
 * @param {*} file - 上传的文件对象
 * @return {*} 返回是否允许上传的布尔值
 */
function field112BeforeUpload(file) {
  // 判断文件大小是否小于2MB
  let isRightSize = file.size / 1024 / 1024 < 2
  if (!isRightSize) {
    ElMessage.error('文件大小超过 2MB')
  }
  return isRightSize
}

/**
 * @name: 表单提交
 * @description: 表单提交方法，验证表单数据并提交
 * @return {*}
 */
function submitForm() {
  // 验证表单
  formRef.value.validate((valid) => {
    if (!valid) return
    // TODO 提交表单
  })
}

/**
 * @name: 表单重置
 * @description: 表单重置方法，将表单数据重置为初始状态
 * @return {*}
 */
function resetForm() {
  formRef.value.resetFields()
}

</script>
<style>
/* 上传提示文字样式 */
.el-upload__tip {
  line-height: 1.2;
}

/* 评分组件样式 */
.el-rate {
  display: inline-block;
  vertical-align: text-top;
}

</style>
